<?php
/**
 * 留言管理
 *
 * @package         YDCMS
 * @author          QQ:644157559 <644157559@qq.com>
 * @version         $Id: MessageController.class.php v1.0 2014-06-13 17:27:03 $
 */
namespace Admin\Controller;
class MessageController extends CommonController{
    private $model;
    private $service;
    public function _initialize() {
        parent::_initialize();
        $this->model = D("Message");
        $this->service = D("Message", "Service");
    }
    
    public function index() {
        $result = $this->getPagination("Message");
        //var_dump($result);die;
        $this->assign("list", $result['data']);
        $this->assign("count", $result['total_rows']);
        $this->assign("page", $result['show']);
        
        $this->display();
    }
    
    /**
     * 查看留言
     */
    public function view() {
        $id = I("get.id", 0, "intval");
        if($id <= 0){
            $this->error("无效操作！");
        }
        $message = $this->service->getById($id);
        if(!isArrayAndNotEmpty($message)){
            $this->error("无效操作！");
        }
        
        $fields = M("Field")->where(array("model_id" => 13))->select();
        $this->assign("addfields", $fields);
        $this->assign("addRow", $message);
        $this->assign($message);
        $this->display("show");
    }
    
    /**
     * 回复留言
     */
    public function reply() {
        $reply = I("post.reply");
        if(!isArrayAndNotEmpty($reply)){
            $this->errorReturn("无效操作！");
        }
        $result = $this->service->reply($reply);
        if(!$result['status']){
            return $this->errorReturn($result['data']['error']);
        }
        
        return $this->successReturn("回复成功!", U("index"));
        
    }
    
    /**
     * 删除
     */
    public function delete() {
        $id = I("post.id", 0, "intval");
        if($id <= 0){
            $this->errorReturn("无效操作！");
        }
        $message = $this->service->getById($id);
        if(!isArrayAndNotEmpty($message)){
            $this->errorReturn("无效操作！");
        }
        
        if($this->service->delete($id)){
            return $this->successReturn("删除成功!");
        }else{
            $this->errorReturn("删除失败！");
        }
    }
    
}
